package indi.wshape1.takeawaydeliveryinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import indi.wshape1.takeawaydeliveryinfo.mapper.SenderMapper;
import indi.wshape1.takeawaydeliveryinfo.pojo.Sender;
import indi.wshape1.takeawaydeliveryinfo.service.SenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wshape1
 * @description 针对表【sender】的数据库操作Service实现
 * @since 2023-01-17 12:57:25
 */
@Service
public class SenderServiceImpl extends ServiceImpl<SenderMapper, Sender>
        implements SenderService {

    @Autowired
    @SuppressWarnings("all")
    private SenderMapper senderMapper;

    @Override
    public boolean verify(String phone, String password) {
        QueryWrapper<Sender> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        Sender sender = getOne(queryWrapper);
        if (sender != null) {
            return password.equals(sender.getPassword());
        }
        return false;
    }

    @Override
    public List<Sender> getListByNameAndKeyword(String blockName, String groupName, String keyword) {

        QueryWrapper<Sender> queryWrapper = new QueryWrapper<>();
        queryWrapper.and((q) ->
                q.like("name", keyword).or()
                        .like("phone", keyword).or()
                        .like("group_name", keyword).or()
                        .like("block_name", keyword));


        if (blockName != null && groupName == null) {
            queryWrapper.and((q) -> q.eq("block_name", blockName));
        } else if (blockName != null) {
            queryWrapper.and((q) ->
                    q.eq("block_name", blockName)
                            .eq("group_name", groupName));
        }
        return list(queryWrapper);
    }

    @Override
    public int freeSenderBatchByGroupIds(List<Integer> ids) {
        return senderMapper.updateBlockGroupBatchByGroupIds("null", "null", ids);
    }

    @Override
    public int freeSenderBatchByBlockIds(List<Integer> ids) {
        return senderMapper.updateBlockGroupBatchByBlockIds("null", "null", ids);
    }
}




